package com.netflexitysolutions.amazonws.s3.internal.operations;

import java.util.List;

import com.amazonaws.s3.doc._2006_03_01.CreateBucket;
import com.amazonaws.s3.doc._2006_03_01.CreateBucketResponse;
import com.netflexitysolutions.amazonws.s3.S3;
import com.netflexitysolutions.amazonws.s3.S3Bucket;
import com.netflexitysolutions.amazonws.s3.S3Grant;
import com.netflexitysolutions.amazonws.s3.internal.utils.S3Utils;

public class CreateBucketOperation extends OperationExecutor<CreateBucket, S3Bucket> {

    private String bucket;
    private List<S3Grant> acl;

    public CreateBucketOperation(S3 s3, String bucket, List<S3Grant> acl){
        super(s3);
        this.bucket = bucket;
        this.acl = acl;
    }

    @Override
    protected S3Bucket call(CreateBucket request) {
//TODO: validate bucket name
        request.setBucket(bucket);
        if(acl != null){
            request.setAccessControlList(S3Utils.convert(acl));
        }
        injectCredentials(request, "CreateBucket", getS3().getAccessKey(), getS3().getSecretKey());
        CreateBucketResponse response = getS3().getService().createBucket(request);

//Bug in amazon service: CreateBucketResult exists xsd, but absents in CreateBucketResponse
        S3Bucket rv = new S3Bucket();
        rv.setName(bucket);
//TODO: add acl to the Bucket
        return rv;

    }
}
